﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

//自定义命名空间
using DreamCube.Apps.Common.WinForm.Controls;
using DreamCube.Apps.Common.WinForm.Controls.DataGridViewCtrl;
using DreamCube.Framework.DataAccess.Basic;
using DreamCube.Foundation.Basic.Utility;

namespace RegexTester.UI
{
    public partial class ViewHistory : Form
    {
        #region "属性"

        public Model.TestHistory SelectHistoryRecord
        {
            get 
            {
                if (dataGridView1.CurrentRow != null)
                {
                    return new Model.TestHistory() {
                         ID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value),
                         Regex = dataGridView1.CurrentRow.Cells["regex"].Value.ToStringEx(),
                         Source = dataGridView1.CurrentRow.Cells["source"].Value.ToStringEx()
                    };
                }
                return null;
            }
        }

        #endregion

        public ViewHistory()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewHistory_Load(object sender, EventArgs e)
        {
            //创建Checkbox
            DataGridViewCheckBoxHeaderHelper.CreateCheckBox(dataGridView1, 0);

            //加载数据
            String columnName = DbColumnAttribute.GetPropertyMapperDBColumnName<Model.TestHistory>("TestDatetime");
            List<Model.TestHistory> testHistory = DAL.TestHistory.Instance.GetModelListEx("", String.Format("{0} desc", columnName),"*", -1);
            if (testHistory != null && testHistory.Count > 0)
            {
                for (Int32 i = 0; i < testHistory.Count; i++)
                {
                    dataGridView1.Rows.Add(new Object[]{
                          false,
                          testHistory[i].ID,
                          testHistory[i].Regex,
                          testHistory[i].Source,
                          testHistory[i].TestDatetime
                    });
                }
            }
        }

        /// <summary>
        /// 返回选中的记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBoxHelper.Information(this, "请选择需要加载的历史记录");
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// 取消按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 删除按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows == null)return;
            if (MessageBoxHelper.YesNo(this, "确定删除选中记录吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                for (Int32 i = dataGridView1.Rows.Count - 1; i >= 0; i--)
                {
                    DataGridViewRow row = dataGridView1.Rows[i];
                    Boolean checkStated = Convert.ToBoolean(row.Cells[0].Value);
                    if (checkStated)
                    {
                        String id = row.Cells[1].Value.ToStringEx();
                        if (DAL.TestHistory.Instance.DeleteEx(String.Format("id={0}", id)) > 0)
                            dataGridView1.Rows.Remove(row);
                    }
                }
            }
        }
    }
}
